package hust.music.action;

import hust.music.action.base.ActionBase;
import hust.music.entity.Music;
import hust.music.entity.User;
import hust.music.service.FileModService;
import hust.music.service.MusicModService;
import hust.music.util.FileHash;
import hust.music.util.Mp3Deal;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * MusicUploadAction
 * 
 * @author Txazo
 * @since 1.0
 */
@Controller
@Scope("session")
public class MusicUploadAction extends ActionBase {

	private static final long serialVersionUID = 1L;

	/** 文件 */
	private File upload;
	/** 文件名称 */
	private String uploadFileName;
	/** 文件类型 */
	private String uploadContentType;
	/** 文件标题 */
	private String title;
	/** 文件说明 */
	private String description;
	/** 文件流派 */
	private String style;
	/** 文件存储路径 */
	private String savePath;
	/** 允许上传的文件类型 */
	private String allowTypes;
	/** 允许上传的文件大小 */
	private long maxSize;

	/** 提示消息 */
	private int tip;

	@Autowired
	private FileModService fileModService;
	@Autowired
	private MusicModService musicModService;

	/**
	 * 上传文件
	 * 
	 * @return 逻辑视图
	 */
	@Override
	public String execute() throws Exception {
		if (this.getUpload().length() < this.getMaxSize()) {
			if (filterTypes(this.getAllowTypes().split(","))) {
				try {
					File file = new File(getSavePath());
					if (!file.exists()) {
						file.mkdir();
					}
					String hashCode = FileHash.getHash(getUpload());
					hust.music.entity.File originalFile = fileModService
							.isFileUploaded(hashCode);
					if (originalFile == null) {
						FileOutputStream fos = new FileOutputStream(file + "//"
								+ hashCode + ".mp3");
						FileInputStream fis = new FileInputStream(getUpload());
						byte[] buffer = new byte[1024];
						int len = 0;
						while ((len = fis.read(buffer)) > 0) {
							fos.write(buffer, 0, len);
						}
						fis.close();
						fos.close();
						hust.music.entity.File musicFile = new hust.music.entity.File();
						musicFile.setHash(hashCode);

						fileModService.addFile(musicFile);

						originalFile = fileModService.isFileUploaded(hashCode);
					}

					Music music = new Music();
					music.setTitle(getTitle());
					music.setDescription(getDescription());
					music.setIsUpload(1);
					music.setListenTimes(0);
					music.setMark(0);
					music.setPath(getPath() + "/" + originalFile.getHash()
							+ ".mp3");
					music.setImage("./images/music/default.jpg");
					music.setUploadTime(new Date());

					Mp3Deal mp3 = new Mp3Deal();
					mp3.getSongInfo(new File(getSavePath() + "\\"
							+ originalFile.getHash() + ".mp3"));
					music.setAlbum(mp3.getAlbum());
					music.setSinger(mp3.getSinger());
					music.setImage(mp3.getImage());
					String[] styles = getStyle().substring(1).split(" ");

					musicModService.uploadMusic(music, (User) session
							.get("user"), originalFile, styles);

					tip = 0;
				} catch (Exception e) {
					System.out.print(e.getMessage());
					tip = 1;
				}
			} else {
				tip = 2;
			}
		} else {
			tip = 3;
		}

		return SUCCESS;
	}

	public boolean filterTypes(String[] types) {
		String fileType = getUploadContentType();
		for (String type : types) {
			if (type.equals(fileType)) {
				return true;
			}
		}
		return false;
	}

	public String getSavePath() throws Exception {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}

	public String getPath() throws Exception {
		return this.savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public int getTip() {
		return tip;
	}

	public void setTip(int tip) {
		this.tip = tip;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getAllowTypes() {
		return allowTypes;
	}

	public void setAllowTypes(String allowTypes) {
		this.allowTypes = allowTypes;
	}

	public long getMaxSize() {
		return maxSize;
	}

	public void setMaxSize(long maxSize) {
		this.maxSize = maxSize;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

}